//
//  GCD_apply_VC.m
//  thread
//
//  Created by 关旭 on 2019/3/11.
//  Copyright © 2019 guanxu. All rights reserved.
//

#import "GCD_apply_VC.h"

@interface GCD_apply_VC ()

@end

@implementation GCD_apply_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self apply];//阻塞主线程
}
//通常我们会用 for 循环遍历，但是 GCD 给我们提供了快速迭代的函数dispatch_apply。dispatch_apply按照指定的次数将指定的任务追加到指定的队列中，并等待全部队列执行结束。
#pragma mark ---------dispatch_apply 可以 在多个线程中同时（异步）遍历多个元素。

//无论是在串行队列，还是异步队列中，dispatch_apply 都会等待全部任务执行完毕，这点就像是同步操作，也像是队列组中的 dispatch_group_wait方法。


- (void)apply {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    NSLog(@"begin");
    dispatch_apply(10, queue, ^(size_t index) {
        int wait = random()%100;
        [NSThread sleepForTimeInterval:wait/10];
        NSLog(@"%zu---%d---%@",index,wait,[NSThread currentThread]);
    });
    NSLog(@"end");

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
